<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-07-07 14:26:50
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\MaterialCategory;
use Illuminate\Support\Facades\DB;

class MaterialCategoryRepository extends Repositories
    {

    public $relation_models = ["creator", "updator"];
    public function boot(MaterialCategory $model)
        {
        $this->model = $model;
        }

    public function list()
        {
        // DB::connection()->enableQueryLog();

        // $this->whereIn("id", [5])
        //     ->with(['menu'])
        //     ->get();
        // dd(DB::getQueryLog());
        // DB::enableQueryLog();
        $list = $this->with(["children", 'material_nature', 'lcategory',"creator", "updator"])->getList();
        // dd(DB::getQueryLog());
        return $list;
        }

    public function delete($ids)
        {
        return treeListCallBack(
            $this->model
                ->with(['children'])
                ->whereIn("id", $ids)
                ->get(),
            function ($model)
                {
                return $model->delete();
                }
        );
        }
    protected function category($categoryid, $array = [])
        {
        $parentid = $this->whereId($categoryid)->value('parent_id');
        if ((int) $parentid > 0) {
            $array[] = $categoryid;
            $array   = array_merge($array, $this->category($parentid, $array));
            return $array;
            }
        return [];
        }
    }